<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit">
        <a-form-item label="姓名" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="text" placeholder="请输入姓名" v-decorator="['name', { rules: [{ required: true, message: '请输入姓名' }] }]"/>
        </a-form-item>
        <a-form-item label="电话" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="number" placeholder="请输入电话" v-decorator="['mobile', { rules: [{ required: true, message: '请输入电话' }] }]"/>
        </a-form-item>
        <a-form-item label="身份证号" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="number" placeholder="请输入身份证号" v-decorator="['idCard', { rules: [{ required: true, message: '请输入身份证号' }] }]"/>
        </a-form-item>
        <a-form-item label="生日" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-date-picker v-decorator="['birthday', { rules: [{ required: true, message: '请输入生日' }] }]"/>
        </a-form-item>
        <a-form-item label="性别" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-select placeholder="请选择性别" v-decorator="['sex', { rules: [{ required: true, message: '请选择性别' }] }]">
            <a-select-option value="1">
              男
            </a-select-option>
            <a-select-option value="2">
              女
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="年龄" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="number" placeholder="请输入年龄" v-decorator="['age', { rules: [{ required: true, message: '请输入年龄' }] }]"/>
        </a-form-item>
        <a-form-item label="身高" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="number" suffix="厘米" placeholder="请输入身高" v-decorator="['height', { rules: [{ required: true, message: '请输入身高' }] }]"/>
        </a-form-item>
        <a-form-item label="体重" :label-col="{ span: labelCol.span }" :wrapper-col="{ span: wrapperCol.span }">
          <a-input type="number" suffix="千克" placeholder="请输入体重" v-decorator="['weight', { rules: [{ required: true, message: '请输入体重' }] }]"/>
        </a-form-item>
        <a-form-item label="选择地区" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <SelectRegion
            placeholder="请选择省市区"
            v-decorator="['address', {rules: [{required: true, message: '请选择省市区'}]}]"
          />
        </a-form-item>

        <a-form-item :wrapper-col="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/project/health'
import { SelectRegion } from '@/components'

export default {
  components: {
    SelectRegion
  },
  data () {
    return {
      // 标签布局属性
      labelCol: { span: 3 },
      // 输入框布局属性
      wrapperCol: { span: 7 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录详情
      record: {},
      //
      address: [1, 2, 3]
    }
  },
  // 初始化数据
  created () {
    // 获取当前详情记录
    // this.getDetail()
  },
  methods: {

    // 获取当前详情记录
    getDetail () {
      this.isLoading = true
      Api.detail()
        .then(result => {
          // 当前记录
          this.record = result.data.values
          // 设置默认值
          this.setFieldsValue()
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, $nextTick, form: { setFieldsValue } } = this
      $nextTick(() => {
        setFieldsValue(pick(record, ['tag']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 提交到后端api
     */
    onFormSubmit (values) {
      this.isLoading = true
      Api.add({ form: values })
        .then((result) => {
          // 显示提示信息
          this.$message.success(result.message, 1.5, () => {
            this.$router.push('/project/manage/health_index')
          })
        })
        .finally((result) => {
          this.isLoading = false
        })
    }

  }
}
</script>
<style lang="less" scoped>
.ant-form-item {
  .ant-form-item {
    margin-bottom: 0;
  }
}

/deep/.ant-form-item-control {
  padding-left: 10px;
  .ant-form-item-control {
    padding-left: 0;
  }
}
</style>
